package com.zzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.domain.LoginUser;
import com.zzx.domain.Permission;
import com.zzx.domain.Role;
import com.zzx.domain.Vo.PermissionVO2;
import com.zzx.domain.Vo.PermissionVo;
import com.zzx.exception.ExceptionState;
import com.zzx.exception.SystemException;
import com.zzx.mapper.PermissionMapper;
import com.zzx.service.PermissionApiService;
import com.zzx.service.PermissionService;
import com.zzx.service.RoleService;
import com.zzx.util.ResponseResult;
import com.zzx.util.SecurityContextHolderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 权限表(Permission)表服务实现类
 *
 * @author makejava
 * @since 2022-07-31 21:40:27
 */
@Service("permissionService")
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionApiService permissionApiService;

    @Override
    public ResponseResult menus() {
        /*获取当前用户*/
        Role role = roleService.getRole();
        if(role.getRoleId()==0){
            List<Permission> list = list();
            List<PermissionVo> permissionVoList = getPermissionVoList(list);
            return new ResponseResult("获取权限列表成功", ExceptionState.OK,permissionVoList);
        }
        /*获取当前角色的所有权限id*/
        List<PermissionVo> permissionVos = permissionVoList(role);
        return new ResponseResult("获取权限列表成功", ExceptionState.OK,permissionVos);
    }

    /**
     * 权限列表
     * @param type
     * @return
     */
    @Override
    public ResponseResult RightsList(String type) {

        /*获取权限列表*/
        List<Permission> list = list();

        List<PermissionVO2> collect = list.stream().map(item ->
                new PermissionVO2(item.getPsId(), item.getPsName(), item.getPsLevel().toString(), item.getPsPid(), permissionApiService.getPath(item.getPsId()), null)
        ).collect(Collectors.toList());
        if(type.equals("list")){
            return new ResponseResult("获取权限列表成功",ExceptionState.OK,collect);
        }else if(type.equals("tree")){
            List<PermissionVO2> collect1 = collect.stream().filter(i ->  "0".equals(i.getLevel())).map(item -> {
                        item.setChildren(
                                permissionVO2List(item.getId(), collect)
                        );
                        return item;
                    }
            ).collect(Collectors.toList());
            return new ResponseResult("获取权限列表成功",ExceptionState.OK,collect1);
        }
            throw new SystemException(ExceptionState.PARAMETERISINVALID);



    }

    /**
     * 分类
     * @param list
     * @return
     */
    public List<PermissionVo> getPermissionVoList(List<Permission> list){
        List<PermissionVo>  result = list.stream().filter(i->i.getPsLevel()==0).map(item->
       new PermissionVo(item.getPsId(),item.getPsName(),permissionApiService.getPath(item.getPsId()),
            permissionVoss(item.getPsId(), list)
       )).collect(Collectors.toList());
        return result;
    }

    public List<PermissionVo> permissionVoss(int psid, List<Permission> permissions){
        List<PermissionVo> collect = permissions.stream().filter(i ->  i.getPsLevel() == 1).filter(ii-> ii.getPsPid().equals(psid)).map(item ->
                new PermissionVo(item.getPsId(), item.getPsName(), permissionApiService.getPath(item.getPsId()),
         permissions.stream().filter(i2->i2.getPsPid() == (int)item.getPsId()&&i2.getPsLevel()==2).map(item2->
                new PermissionVo(item2.getPsId(),item2.getPsName(),permissionApiService.getPath(item2.getPsId()),null)).collect(Collectors.toList())
                )).collect(Collectors.toList());
        return  collect;
    }

    public List<PermissionVO2> permissionVO2List(int id,List<PermissionVO2> permissionVO2List){
        return permissionVO2List.stream().filter(i2 -> "1".equals(i2.getLevel())&&i2.getPid().equals(id)).map(item2 -> {
            item2.setChildren(
                    permissionVO2List.stream().filter(i3 ->  "2".equals(i3.getLevel()) && i3.getPid().equals(item2.getId())).collect(Collectors.toList())
            );
            return item2;
        }).collect(Collectors.toList());

    }

    public List<PermissionVo> permissionVoList(Role role){
        String list = role.getPsIds();
        /*转换成数组*/
        String[] str = list.split(",");
        List<String> permissionId = Arrays.asList(str);
        /*通过权限id去获取权限*/
        List<Permission> permissions = listByIds(permissionId);
        List<PermissionVo> permissionVoList = getPermissionVoList(permissions);
        return permissionVoList;
    }
}

